package com.xfly.app.ui;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ptmall.app.utils.SharedPreferenceUtil;

import java.io.File;
import java.io.FileNotFoundException;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;


/**
 * Created by Administrator on 2017/12/29.
 * vueJs交互规约类
 */

public class AndroidtoJs extends Object {

    public static final String METHOD_NAME = "androidapp";
    public static String KEY_PARAMS_ID = "id";//支付相关id 如 订单编号等
    public static String KEY_PARAMS_ORDER_TYPE = "orderType";//订单类型 默认值DEFAULT_ORDER_TYPE
    public static String KEY_PARAMS_MONEY = "money";//支付金额
    public static String KEY_PARAMS_FROM_PAGE = "fromPage";//跳转的界面
    Context mContext;
    public static String FROM_PAGE_ZHONGCHOU = "zhongchouPage";//众筹订单界面过来的
    private String from;
    private Bitmap bitmap = null;

    private Handler mHandler = new Handler();

    private Gson gson = new GsonBuilder()
            //.setLenient()// json宽松
            //.enableComplexMapKeySerialization()//支持Map的key为复杂对象的形式
            //.serializeNulls() //智能null
            //.setPrettyPrinting()// 调教格式
            .disableHtmlEscaping() //默认是GSON把HTML 转义的
            .create();


    /**
     * 申请升级   upgrade
     * 我的发货订单  forder
     * 进货  purchase
     * 我的店铺 myshop
     * 团队管理 team
     * 合同管理  contract
     * 个人资料 myinfo
     *
     * */
    public AndroidtoJs(Context mContext, String from) {
        this.mContext = mContext;
        this.from = from;

    }

    @JavascriptInterface
    public void toCall(String mobile) {
        Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mobile));//跳转到拨号界面，同时传递电话号码
        mContext.startActivity(dialIntent);
    }

    @JavascriptInterface
    public void loginout(String data) {

        Log.i("xu",data+"------------------");
        Intent intent = new Intent(mContext,XfLoginActivity.class);
        mContext.startActivity(intent);

        SharedPreferenceUtil.setSharedStringData(mContext,"token","");
        ((Activity)mContext).finish();
    }
    @JavascriptInterface
    public void myinfo(String data) {

        Intent intent = new Intent(mContext,SetActivity.class);
        mContext.startActivity(intent);

    }
    @JavascriptInterface
    public void purchase(String data) {

        Intent intent = new Intent(mContext,JhActivity.class);
        mContext.startActivity(intent);

    }
    @JavascriptInterface
    public void contract(String data) {

        Intent intent = new Intent(mContext,HtActivity.class);
        mContext.startActivity(intent);

    }
    @JavascriptInterface
    public void team(String data) {

        Intent intent = new Intent(mContext,TeamActivity.class);
        mContext.startActivity(intent);

    }
    @JavascriptInterface
    public void upgrade(String data) {

        Intent intent = new Intent(mContext,SjActivity.class);
        mContext.startActivity(intent);

    }
    @JavascriptInterface
    public void myshop(String data) {

        Intent intent = new Intent(mContext,StoreActivity.class);
        mContext.startActivity(intent);

    }
    @JavascriptInterface
    public void forder(String data) {

        Intent intent = new Intent(mContext,XfOrdersActivity.class);
        mContext.startActivity(intent);

    }
    @JavascriptInterface
    public void wallet(String data) {

        Intent intent = new Intent(mContext,WalletActivity.class);
        mContext.startActivity(intent);

    }
    @JavascriptInterface
    public void toaddress(String data) {
        TjActivity.ADDRESSID =data;
        ((Activity)mContext).finish();
    }
    @JavascriptInterface
    public void viewend(String data) {
        Log.i("xu",data+"------------------");
        ((Activity)mContext).finish();
    }

    @JavascriptInterface
    public void toPay(String orderId, String tableName) {
//        Intent intent = new Intent(mContext, PaymentOptions.class);
//        intent.putExtra(PaymentOptions.KEY_PARAMS_ID, orderId);
//        intent.putExtra(PaymentOptions.KEY_PARAMS_ORDER_TYPE, tableName);
//        intent.putExtra(PaymentOptions.KEY_PARAMS_MONEY, "");
//        intent.putExtra(PaymentOptions.KEY_PARAMS_FROM_PAGE, from);
//        mContext.startActivityForResult(intent, REQUST_CODE_PAY);
//        Intent intent = new Intent(mContext, PaymentOptions.class);
//        intent.putExtra("id", orderId);
//        intent.putExtra("orderType", tableName);
        if (mContext instanceof Web && from.equals(FROM_PAGE_ZHONGCHOU)) {
            ((Web) mContext).finish();
        }
        //mContext.startActivity(intent);
    }




    public static void shareImage(Context context, Uri uri, String title) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.setType("image/jpeg");
        context.startActivity(Intent.createChooser(shareIntent, title));
    }
    /**
     * ContentProvider 的方式也可能失败
     * 使用ContentUri分享的方式 是可以分享到 部分应用获取资源失败的情况，比如 朋友圈
     *
     * @param context
     * @param filePath
     * @return
     */
    public static Uri getImageContentUri(Context context, String filePath) {
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=? ",
                new String[]{filePath}, null);
        Uri uri = null;

        if (cursor != null) {
            if (cursor.moveToFirst()) {
                int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
                Uri baseUri = Uri.parse("content://media/external/images/media");
                uri = Uri.withAppendedPath(baseUri, "" + id);
            }

            cursor.close();
        }

        if (uri == null) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, filePath);
            uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        }

        return uri;
    }




}